﻿<?php 
/*
	function.php 
	auther: 罗驰 
	email:yemasky@msn.com
*/
//DB_HOST, DB_USER, DB_PASSWORD
define('DB_HOST', '127.0.0.1');
define('DB_USER', 'root');
define('DB_PASSWORD', 'root');
define('DB_NAME', 'reading');
define('DB_TABLE', '');
define('__CHARSET', 'UTF-8');
try {

if(!empty($_POST['add']) && !empty($_POST['name']) && !empty($_POST['fun']) && $_POST['fsid'] != '') {
	$sql = "INSERT INTO ".DB_TABLE."functions (function_name, function_en_name, parent, orderid, functions) VALUES ('"
		  . $_POST['name'] ."','".$_POST['enname']."','".$_POST['fsid']."','".$_POST['orderid']."','".$_POST['fun']."')";
	DBQuery::DB()->execute($sql);
}
if(!empty($_POST['modify'])) {
	if(is_array($_POST['id']) && is_array($_POST['name'])) {
		$arrId = $_POST['id'];
		$arrOid = $_POST['oid'];
		$arrName = $_POST['name'];
		$arrEnName = $_POST['enname'];
		$arrFun = $_POST['fun'];
		$arrFsid = $_POST['fsid'];
		$arrOrderid = $_POST['orderid'];
		$arrShow =  $_POST['show'];
		
		$num = count($arrId);
		$numOid = count($arrOid);
		
		for($i = 0; $i < $num; $i++) {
			for($j = 0; $j <$numOid; $j++) {
				if($arrId[$i] == $arrOid[$j]) {
					$arrShow[$j] = empty($arrShow[$j]) ? '0' : '1';
					$sql = "UPDATE ".DB_TABLE."functions SET function_name = '".$arrName[$j]
						  ."', function_en_name = '".$arrEnName[$j]."', functions = '".$arrFun[$j]
						  ."', parent = '".$arrFsid[$j]."', orderid = '".$arrOrderid[$j]
						  ."', display = '".$arrShow[$j]
						  ."' WHERE fid = '".$arrId[$i]."';";
					DBQuery::DB()->execute($sql);
				}
			}
		}
	}
}

$sql = "SELECT * FROM ".DB_TABLE."functions ORDER BY parent, orderid;";
$rs = DBQuery::DB()->getList($sql);
} catch (Exception $e) {
	print_r($e -> getMessage());
}
?>
<table width="100%" border="1">
  <tr>
    <td>ID</td>
    <td>display</td>
    <td>function_name</td>
    <td>function_en_name</td>
    <td>parent</td>
    <td>orderid</td>
    <td>functions</td>
  </tr>
<form action="" method="post">
<?php 
$num = count($rs);
for($i = 0; $i< $num; $i++) {
?>
  <tr>
    <td><input name="oid[]" type="hidden" value="<?php echo $rs[$i]['fid']; ?>" />
        <input name="id[]" type="checkbox" value="<?php echo $rs[$i]['fid']; ?>" />
		<?php echo $rs[$i]['fid']; ?></td>
    <td><input type="checkbox" value="0" onclick="setShow(this, 'show<?php echo $rs[$i]['fid']; ?>')" <?php if($rs[$i]['display']=='1'){?> checked<?php }?> />
        <input name="show[]" type="hidden" value="<?php echo $rs[$i]['display']; ?>" id="show<?php echo $rs[$i]['fid']; ?>" /></td>
    <td><input name="name[]" type="text" value="<?php echo $rs[$i]['function_name'];  ?>" /></td>
    <td><input name="enname[]" type="text" value="<?php echo $rs[$i]['function_en_name'];  ?>" size="12" /></td>
    <td><input name="fsid[]" type="text" value="<?php echo $rs[$i]['parent'];  ?>" size="5" /></td>
    <td><input name="orderid[]" type="text" value="<?php echo $rs[$i]['orderid'];  ?>" size="5" /></td>
    <td><input name="fun[]" type="text" value="<?php echo $rs[$i]['functions'];  ?>" size="20" /></td>
  </tr>
<?php 
}
?>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td><input type="submit" name="modify" value="提交" /></td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</form>
<form action="" method="post">
  <tr>
    <td></td>
    <td>&nbsp;</td>
    <td><input name="name" type="text" value="" /></td>
    <td><input name="enname" type="text" value="" size="12" /></td>
    <td><input name="fsid" type="text" value="" size="5" /></td>
    <td><input name="orderid" type="text" value="" size="5" /></td>
    <td><input name="fun" type="text" value="" size="20" /></td>
  </tr>
  <tr>
    <td></td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td><input type="submit" name="add" value="提交" /></td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</form>
</table>
<script laguage="javascript">
function setShow(obj, id) {
	if(obj.checked==true) {
		document.getElementById(id).value = 1;
	} else {
		document.getElementById(id).value = 0;
	}
}
</script>

<?php
function getMicrotime() {
	return 1;
}
function logDebug(){
}
class SQLException extends Exception { }

class DBQuery {
	
	private static $conn;
	private static $DB;
	
    public function __construct() {
		$this -> connect();
    }
    private function connect() {
       	if(self::$conn == NULL) {
			$startTime = getMicrotime();
			$this -> CConnect(DB_HOST, DB_USER, DB_PASSWORD);
			logDebug("connect use time: " . (getMicrotime() - $startTime));
		}
		if(self::$conn == false) {
			throw new SQLException("can't connect database, please check setting.");	
		}
		if(mysql_select_db(DB_NAME, self::$conn) == false) {
			throw new SQLException("can't select db, please check setting.");	
		}
        if (0 == strcmp(strtoupper(__CHARSET), strtoupper("UTF-8"))) {
			$this->_execute("SET NAMES UTF8;");
        	/*$this->_execute("SET CHARACTER_SET_CLIENT = utf8, " .
        					"CHARACTER_SET_CONNECTION = utf8, " .
        					"CHARACTER_SET_DATABASE   = utf8, " .
        					"CHARACTER_SET_RESULTS    = utf8, " .
        					"CHARACTER_SET_SERVER     = utf8, " .
        					"COLLATION_CONNECTION     = utf8_general_ci, " .
        					"COLLATION_DATABASE       = utf8_general_ci, " .
        					"COLLATION_SERVER         = utf8_general_ci ");*/
        }
	}
	
	public static function DB() {
		if(is_object(self::$DB)) { //and is_object(self::$DB)) {
			return self::$DB;
		}
		return self::$DB = new DBQuery;
	}

	/*将数组转换到以','分隔的字符串*/
	public static function arrSqlToSString($arr) {
		if(!is_array($arr)) {
			return $arr;
		}
		$str = $arr[0];
		$num = count($arr);
		for($loop=1; $loop<$num; $loop++) {
			$str .= ',' . $arr[$loop];
		}
		return $str;
	}

    public function getOne($sql) {
		if($arrOne = mysql_fetch_row($this-> _execute($sql))) {
			return $arrOne[0];
		}
		return NULL;
    }
    
    public function getList($sql) {
		$rs = NULL;
    	if($list = $this-> _execute($sql)) {
			while($row = mysql_fetch_array($list, MYSQL_ASSOC)) {
			   $rs[] = $row;
			}
		}
		return $rs;
    }
    
    public function getRow($sql) {
    	if($row = $this-> _execute($sql)) {
			return mysql_fetch_array($row, MYSQL_ASSOC);
		}
		return NULL;
    }
    
    public function getInsertID() {
    	if($id = mysql_insert_id()) {
			return $id;
		}
		return NULL;
    }
    
    public function execute($sql) {
    	return $this->_execute($sql);
    }//modify delete insert update
        
	public function mysqlVer() {
		return mysql_get_server_info();
	}

	public function close() {
		return mysql_close();
	}

    private function _execute($sql) {
		if (false == ($result = mysql_query($sql, self::$conn))) {
			throw new SQLException("error: SQL=$sql");
		}
		return $result;
	}
	
	private function CConnect($host, $username, $password) {
		if(self::$conn == NULL) {
			if((self::$conn = mysql_connect($host, $username, $password)) == false) {
				throw new SQLException("can't link to db host, please check setting.");	
			}
		}
		return self::$conn;
	} 

	private function PConnect($host, $username, $password) {
		if(self::$conn == NULL) {
			if((self::$conn = mysql_pconnect($host, $username, $password)) == false) {
				throw new SQLException("can't link to db host, please check setting.");	
			}
		}
		return self::$conn;
	} 
 }
 ?>